package com.SmartCare.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.SmartCare.common.Result;
import com.SmartCare.service.ActivityRecordService;
import com.SmartCare.common.vo.ActivityRecordVo;
import com.SmartCare.pojo.ActivityRecord;
import com.SmartCare.mapper.ActivityRecordMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.SmartCare.utils.DateTimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 活动情况表 服务实现类
 */
@Service
public class ActivityRecordServiceImpl extends ServiceImpl<ActivityRecordMapper, ActivityRecord> implements ActivityRecordService {

    @Override
    public Result getInfo(Long elderlyId, String date) {
        LambdaQueryWrapper<ActivityRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ActivityRecord::getElderlyId,elderlyId);
        queryWrapper.like(ActivityRecord::getCreatedTime,date);
        queryWrapper.orderByAsc(ActivityRecord::getCreatedTime);

        List<ActivityRecord> list = list(queryWrapper);
        if(list.isEmpty()){
            return Result.error("无记录");
        }
        List<ActivityRecordVo> res=new ArrayList<>();
        list.forEach((item)->{
            ActivityRecordVo vo = new ActivityRecordVo();
            BeanUtils.copyProperties(item,vo,"pictureUrl");
            vo.setCreatedTime(DateTimeUtils.formatDateTime(item.getCreatedTime()));
            vo.setPictureUrl(Arrays.asList(item.getPictureUrl().split(";")));
            res.add(vo);
        });

        return Result.success(res);
    }

    @Override
    public Result add(Long elderlyId, Long nurseId, String activityType, Float duration, List<String> pictureUrls) {
        ActivityRecord record = ActivityRecord.builder()
                .elderlyId(elderlyId)
                .activityType(activityType)
                .nurseId(nurseId)
                .duration(duration)
                .pictureUrl(String.join(";", pictureUrls))
                .build();
        save(record);

        return Result.success("添加记录成功");
    }
}
